﻿using Arango.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestArangoDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// add static method to append log to richtext box.
        /// </summary>
        public async void AppendText(string str)
        {

            await Task.Run(() =>
            {
                Console.WriteLine("");
                this.Invoke((Action)delegate
                {
                    this.richTextBox1.AppendText(str + "\r\n");
                });

            });

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Connection management
                ASettings.AddConnection(
                    "testdb1", "127.0.0.1", 8529, false, "testdb1", "tiger", "hzh1660");
                ASettings.AddConnection(
                    "myapp", "127.0.0.1", 8529, false, "myapp", "tiger", "hzh1660");
                //Checking and removing existing connections
                if (ASettings.HasConnection("someDatabaseAlias"))
                {
                    // removes specified connection
                    ASettings.RemoveConnection("someDatabaseAlias");
                }
                //Database context 
                //There is no need to dispose ADatabase instance in order to free database connection because operations are performed through HTTP calls.
                // initialize new database context
                var db = new ADatabase("testdb1");
                var currentDatabaseResult = db.GetCurrent();
                AppendText("connect to db testdb1" + currentDatabaseResult.Success);
                if (currentDatabaseResult.Success)
                {
                    var name = currentDatabaseResult.Value.String("name");
                    var id = currentDatabaseResult.Value.String("id");
                    var path = currentDatabaseResult.Value.String("path");
                    var isSystem = currentDatabaseResult.Value.Bool("isSystem");
                    AppendText(path);
                    AppendText("db name is " + name);
                }



                var createCollectionResult = db.Collection.KeyGeneratorType(AKeyGeneratorType.Autoincrement).WaitForSync(true).Create("MyDocumentCollection");
                AppendText(" create collection  is " + createCollectionResult.Success);
                if (createCollectionResult.Success)
                {
                    AppendText(createCollectionResult);
                }
                var getCollectionResult = db.Collection.Get("MyDocumentCollection");
                AppendText(" getCollectionResult MyDocumentCollection  is " + getCollectionResult.Success);

                if (getCollectionResult.Success)
                {
                    AppendText(getCollectionResult);
                }
                AppendText("321");
                //  Arango.fastJSON.JSON.ToJSON(new )
                FileStream fs = File.OpenRead(@"c:/temp/1.jpg");
                byte[] _bs=File.ReadAllBytes(@"c:/temp/1.jpg");
                string _str = Arango.fastJSON.JSON.ToJSON(_bs);
                
                AppendText(_str);
                var _s2=Arango.fastJSON.JSON.ToObject(_str,typeof(byte[]));
                _bs = (byte[])_s2;
                File.WriteAllBytes(@"c:/temp/2.jpg", _bs);

                AppendText("123");



                var getAllCollectionResult = db.GetAllCollections();

                AppendText(" get All CollectionResult is " + getAllCollectionResult.Success);
                if (getAllCollectionResult.Success)
                {
                    var count = getAllCollectionResult.Value.Count();
                    AppendText(" Collection count:" + count);
                }
                var deleteCollectionResult = db.Collection.Delete("MyDocumentCollection");

                if (deleteCollectionResult.Success)
                {
                    var id = deleteCollectionResult.Value.String("id");
                    AppendText("delete MyDocumentCollection id:"+id);
                }

                getCollectionResult = db.Collection.GetCount("TestCollection01");

                AppendText(" getCollectionResult is " + getCollectionResult.Success);
                if (getCollectionResult.Success)
                {
                    AppendText(getCollectionResult);
                }

                // retrieve specified document
                //var getResult = db.Document.Get("testcollection01");
                //  AppendText("get testcollection01 query");
                // AppendText(getResult.ToString());
                AppendText("end");

            }
            catch (Exception exp)
            {
                AppendText(exp.Message);

            }
        }

        private void AppendText(AResult<Dictionary<string, object>> collectionResult)
        {
            var id = collectionResult.Value.String("id");
            var name = collectionResult.Value.String("name");
            //var waitForSync = collectionResult.Value.Bool("waitForSync");
            //var isVolatile = collectionResult.Value.Bool("isVolatile");
            //var isSystem = collectionResult.Value.Bool("isSystem");
            //var status = collectionResult.Value.Enum<ACollectionStatus>("status");
            //var type = collectionResult.Value.Enum<ACollectionType>("type");
            //var doCompact = collectionResult.Value.Bool("doCompact");
            //var journalSize = collectionResult.Value.Long("journalSize");
            //var keyGeneratorType = collectionResult.Value.Enum<AKeyGeneratorType>("keyOptions.type");
            //var allowUserKeys = collectionResult.Value.Bool("keyOptions.allowUserKeys");
            //var count = collectionResult.Value.Long("count");
            AppendText("id=" + id);
            AppendText("name=" + name);

        }
    }
}
